package com.younion.action.manage;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.json.annotations.JSON;

import com.younion.business.ICardCountInitService;
import com.younion.business.IProductService;
import com.younion.common.base.BaseAction;
import com.younion.common.paginator.Paginator;
import com.younion.vo.ActionResultJsonVO;
import com.younion.vo.CardCountInitVO;
import com.younion.vo.CustomerVO;
import com.younion.vo.ProductVO;

import cpcns.jhsks.util.MapCollectionUtils;

/**
 * @author wells
 */
public class CardCountInitAction extends BaseAction {

	private static final long serialVersionUID = 1L;

	protected static final Log logger = LogFactory
			.getLog(CardCountInitAction.class);

	private ICardCountInitService cardCountInitService = null;
	
	private IProductService productService = null;

	private CardCountInitVO cardCountInitVO = new CardCountInitVO();

	private List<CardCountInitVO> cardCountInitList;
	
	private List<ProductVO> productList;
	
	private ActionResultJsonVO actionResultJsonVO = new ActionResultJsonVO();
	

	/**
	 * 列表：翻页区；数据区
	 */
	@SuppressWarnings("unchecked")
	public String listCardCountInit() {
		try {
			String cardEntityId = request.getParameter("cardEntityId");
			String customerId = request.getParameter("customerId");
			Map paramMap = null;
			if (cardCountInitVO != null) {
				if(StringUtils.isNotEmpty(cardEntityId)){
					cardCountInitVO.setCardEntityId(new Integer(cardEntityId));
				}
				paramMap = MapCollectionUtils.removeEmptyEntry(PropertyUtils
						.describe(cardCountInitVO));
			}
			Map productParamMap = new HashMap();
			CustomerVO customerVO = new CustomerVO();
			customerVO.setId(Integer.parseInt(customerId));
			productParamMap.put("customerVO", customerVO);
			productList = (List<ProductVO>) productService.getByMap(productParamMap);
			// 获取总记录数
			int total = cardCountInitService.getCountByMap(paramMap);
			// 根据总记录数获取分页对象
			Paginator paginator = this.getPaginator(total);
			cardCountInitList = cardCountInitService.get(paramMap, paginator);
			// 保存分页对象
			this.savaPaginator(paginator);
			request.setAttribute("customerId", customerId);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
		return SUCCESS;
	}

	/**
	 * 删除
	 */
	public String deleteCardCountInit() {
		try {
			String pkId = request.getParameter("key");
			cardCountInitService.remove(Arrays.asList(pkId.split(",")));
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			e.printStackTrace();
		}
		return SUCCESS;
	}


	public ICardCountInitService getCardCountInitService() {
		return cardCountInitService;
	}


	public void setCardCountInitService(ICardCountInitService cardCountInitService) {
		this.cardCountInitService = cardCountInitService;
	}


	public CardCountInitVO getCardCountInitVO() {
		return cardCountInitVO;
	}


	public void setCardCountInitVO(CardCountInitVO cardCountInitVO) {
		this.cardCountInitVO = cardCountInitVO;
	}


	public List<CardCountInitVO> getCardCountInitList() {
		return cardCountInitList;
	}


	public void setCardCountInitList(List<CardCountInitVO> cardCountInitList) {
		this.cardCountInitList = cardCountInitList;
	}

	public void setProductService(IProductService productService) {
		this.productService = productService;
	}


	public List<ProductVO> getProductList() {
		return productList;
	}


	public void setProductList(List<ProductVO> productList) {
		this.productList = productList;
	}

	@JSON(name="result")
	public ActionResultJsonVO getActionResultJsonVO() {
		return actionResultJsonVO;
	}


	public void setActionResultJsonVO(ActionResultJsonVO actionResultJsonVO) {
		this.actionResultJsonVO = actionResultJsonVO;
	}
}